package cs304.p3.library.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.google.common.collect.Maps;

import cs304.p3.library.controller.LoginController;
import cs304.p3.library.controller.MainController;
import cs304.p3.library.controller.MainController.Command;
import cs304.p3.library.dao.DevDao;
import cs304.p3.library.framework.IoC;
import cs304.p3.library.framework.LibraryDataAccessContext;
import cs304.p3.library.framework.ViewResolver;
import cs304.p3.library.model.DBTableModel;
import cs304.p3.library.model.DBTableModel.TableType;
import cs304.p3.library.model.LoginForm;
import cs304.p3.library.view.DBObjectDialog.DBOActionType;

@SuppressWarnings("serial")
public class MainFrame extends JFrame{
	
	private static final String TITLE = "Library Database Manager 2.0";

	private JTextArea textArea;
	private JTextField commandLine;
	private String[] allFontNames, tableNames;
	private Font displayFont, cmdFont;
	private JSplitPane splitPane;
	private JScrollPane tableScrollPane = null;
	private DBTableView borrowerTable, borrower_typeTable, bookTable,
						hasSubjectTable, hasAuthorTable, bookCopyTable,
						holdRequestTable, borrowingTable, fineTable, currentTable = null;
	private String previousCmd = "";
	
	private MainController mainController = IoC.resolve(MainController.class);
	
	private LoginController loginController = IoC.resolve(LoginController.class);
	private LoginForm loginForm = loginController.getLoginForm();

	public MainFrame(){
		super.frameInit();
		setTitle(TITLE);
		setMinimumSize(new Dimension(800, 600));
		JPanel contentPane = new JPanel();
		setContentPane(contentPane);
		Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
		allFontNames = new String[fonts.length];
		for(int i=0; i<fonts.length; i++){
			allFontNames[i] = fonts[i].getName();
		}
		cmdFont = new Font("Verdana", Font.PLAIN, 18);
		displayFont = new Font("Verdana", Font.PLAIN, 18);
		try {
			List<String> tableNameList = IoC.resolve(DevDao.class).selectTables();
			tableNames = new String[tableNameList.size()];
			for(int i=0; i<tableNames.length; i++) tableNames[i] = tableNameList.get(i);
		} catch (SQLException e1) {
			System.err.println("Could not Fetch Table Names!\nUsing Default names");
			e1.printStackTrace();
			tableNames = new String[9];
			tableNames[0] = "Borrower";
			tableNames[1] = "Borrower Type";
			tableNames[2] = "Book";
			tableNames[3] = "Has Author";
			tableNames[4] = "Has Subject";
			tableNames[5] = "Book Copy";
			tableNames[6] = "Hold Request";
			tableNames[7] = "Borrowing";
			tableNames[8] = "Fine";
		}

		//Setup Menus
		setupMenus();

		//Add text area and command bar
		contentPane.setLayout(new BorderLayout());
		textArea = new JTextArea();
		textArea.setFont(displayFont);
		textArea.setEditable(false);
		textArea.setEnabled(false);
		textArea.setDisabledTextColor(Color.black);
		textArea.setLineWrap(true);
		textArea.setText("Welcome to " + TITLE + "!");
		commandLine = new JTextField();
		commandLine.setFont(cmdFont);
		commandLine.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {

			}

			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					String stringCmd = commandLine.getText();
					previousCmd = stringCmd;
					Command cmd = mainController.parseCmd(stringCmd.toLowerCase());
					String[] args = cmd.getArgs();
					String msg = cmd.getMessage();
					DevDao devDao = IoC.resolve(DevDao.class);
					switch(cmd.getType()){
					case HELP:
						appendText(msg);
						break;
					case CLEAR:
						clearText();
						break;
					case TYPES:
						appendText(msg);
						break;
					case SWITCH:
						appendText(msg);
						if(args[0].equals(loginForm.getUsername())){
							appendText("You are already logged in\n");
							break;
						}
						loginForm.setUsername(args[0]);
						loginForm.setPassword(args[1]);
						loginController.login();
						if(loginForm.getUsername().equals(LibraryDataAccessContext.DEFAULT_USERNAME)){
							appendText("Invalid Login\nUsing default login...\n");
						}else appendText("Welcome user: " + loginForm.getUsername() + "\n");
						updateDBTables();
						break;
					case SETURL:
						appendText(msg);
						loginForm.setJdbcUrl(args[0]);
						loginController.login();
						appendText("URL set to: " + loginForm.getJdbcUrl() + "\n");
						break;
					case INSERT:
						if(getTableModel(cmd.getTableType()).insertData(args)){
							try {
								devDao.insert(cmd.getTableType(), args);
							} catch (SQLException sqle) {
								sqle.printStackTrace();
							}
						}
						break;
					case UPDATE:
						//TODO actually update in db
						break;
					case DELETE:
						Map<String, String> argmap = Maps.newHashMap();
						String[] labels = cmd.getTableType().getLabels();
						if(args.length != labels.length) break;
						for(int i=0; i<args.length; i++){
							argmap.put(labels[i], args[i]);
						}
						try {
							devDao.delete(cmd.getTableType(), argmap);
						} catch (SQLException sqle) {
							JOptionPane.showMessageDialog(MainFrame.this, sqle.toString(), "Delete Error", JOptionPane.ERROR_MESSAGE);
							sqle.printStackTrace();
						}
						updateDBTables();
						break;
					case DISPLAY:
						setTable(cmd.getTableType());
						break;
					case INSERTVIEW:
						appendText(msg);
						resolveDBObjecViews(args[0], DBOActionType.INSERT);
						break;
					case UPDATEVIEW:
						appendText(msg);
						resolveDBObjecViews(args[0], DBOActionType.UPDATE);
						break;
					case DELETEVIEW:
						appendText(msg);
						resolveDBObjecViews(args[0], DBOActionType.DELETE);
						break;
					case EXIT:
						appendText(msg);
						closeApplication();
						break;
					case INVALID:
						appendText(msg);
						break;
					}
					commandLine.setText("");
				}else if(e.getKeyCode() == KeyEvent.VK_UP){
					commandLine.setText(previousCmd);
				}
			}

			@Override
			public void keyTyped(KeyEvent e) {

			}

		});
		JPanel commandPanel = new JPanel();
		commandPanel.setLayout(new BorderLayout());
		commandPanel.add(new JScrollPane(textArea), BorderLayout.CENTER);
		commandPanel.add(commandLine, BorderLayout.SOUTH);
		commandPanel.setMinimumSize(new Dimension(320, 240));
		commandPanel.setPreferredSize(new Dimension(640, 480));
		//contentPane.add(cmdPanel, BorderLayout.CENTER);
		
		//Update And Create Table View
		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, commandPanel, tableScrollPane);
		updateDBTables();
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		//Center the application
		Dimension d = getToolkit().getScreenSize();
		Rectangle r = getBounds();
		setLocation( (d.width - r.width)/2, (d.height - r.height)/2 );
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowListener(){

			@Override
			public void windowActivated(WindowEvent e) {
			}

			@Override
			public void windowClosed(WindowEvent e) {
			}

			@Override
			public void windowClosing(WindowEvent e) {
				//System.out.println("Properly Closed");
				closeApplication();
			}

			@Override
			public void windowDeactivated(WindowEvent e) {
			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				commandLine.requestFocus();
			}

			@Override
			public void windowIconified(WindowEvent e) {
			}

			@Override
			public void windowOpened(WindowEvent e) {
			}

		});
		commandLine.requestFocus();
	}

	private void setupMenus(){
		JMenuBar bar = new JMenuBar();

		//Application Menu
		JMenu application = new JMenu("Application");
		application.setMnemonic(KeyEvent.VK_A);
		//Add the login menu item
		JMenuItem loginMenuItem = new JMenuItem("Switch");
		loginMenuItem.setMnemonic(KeyEvent.VK_S);
		loginMenuItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ViewResolver.gotoLoginView();
			}
		});
		application.add(loginMenuItem);
		//Add the exit menu item
		JMenuItem exit = new JMenuItem("Exit");
		exit.setMnemonic(KeyEvent.VK_X);
		exit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				closeApplication();
			}
		});
		application.add(new JSeparator());
		application.add(exit);
		bar.add(application);

		//Setting Menu
		JMenu settings = new JMenu("Settings");
		settings.setMnemonic(KeyEvent.VK_E);
		//URL menu item
		JMenuItem changeURL = new JMenuItem("Connection URL");
		changeURL.setMnemonic(KeyEvent.VK_R);
		changeURL.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String connectionString = "";
				connectionString = (String)JOptionPane.showInputDialog(null, "Possible Selections:", "URL Selection", JOptionPane.QUESTION_MESSAGE, null, LibraryDataAccessContext.connectionStrings, LibraryDataAccessContext.connectionStrings[0]);
				if(connectionString == null) connectionString = LibraryDataAccessContext.connectionStrings[0];
				appendText("Connection URL set to: " + connectionString);
				loginForm.setJdbcUrl(connectionString);
				loginController.login();
				appendText("Successfully Reconnected with new String");
			}
		});
		settings.add(changeURL);
		//Display font menu item
		JMenuItem displayFontMenuItem = new JMenuItem("Display Font");
		displayFontMenuItem.setMnemonic(KeyEvent.VK_F);
		displayFontMenuItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String newFontName = (String)JOptionPane.showInputDialog(null, "Fonts:", "Fonts", JOptionPane.QUESTION_MESSAGE, null, allFontNames, allFontNames[0]);
				if(newFontName == null) newFontName = "Verdana";
				displayFont = new Font(newFontName, displayFont.getStyle(), displayFont.getSize());
				textArea.setFont(displayFont);
			}
		});
		//settings.add(new JSeparator());
		settings.add(displayFontMenuItem);
		//Display font size menu item
		JMenuItem displayFontSizeMenuItem = new JMenuItem("Display Font Size");
		displayFontSizeMenuItem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String input = JOptionPane.showInputDialog("Enter Size:");
				int size = 0;
				try{
					size = Integer.parseInt(input);
				}catch(NumberFormatException ex){
					size = 18;
				}
				displayFont = new Font(displayFont.getName(), displayFont.getStyle(), size);
				textArea.setFont(displayFont);
			}
		});
		//settings.add(new JSeparator());
		settings.add(displayFontSizeMenuItem);
		bar.add(settings);
		
		//View menu
		JMenu viewMenu = new JMenu("View");
		viewMenu.setMnemonic(KeyEvent.VK_V);
		//Tables sub-menu
		JMenu tableMenu = new JMenu("Tables");
		//tables sub-menu items
		//Borrower
		JMenuItem borrowerTableMenu = new JMenuItem("Borrower");
		borrowerTableMenu.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				setTable(TableType.BORROWER);
			}
			
		});
		tableMenu.add(borrowerTableMenu);
		//Borrower_type
		JMenuItem borrowerTypeTableMenu = new JMenuItem("Borrower Type");
		borrowerTypeTableMenu.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				setTable(TableType.BORROWERTYPE);
			}
			
		});
		//tableMenu.add(new JSeparator());
		tableMenu.add(borrowerTypeTableMenu);
		//Book
		JMenuItem bookTableMenu = new JMenuItem("Book");
		bookTableMenu.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				setTable(TableType.BOOK);
			}
			
		});
		//tableMenu.add(new JSeparator());
		tableMenu.add(bookTableMenu);
		//Has Author
		JMenuItem hasAuthorTableMenu = new JMenuItem("Has Author");
		hasAuthorTableMenu.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				setTable(TableType.HASAUTHOR);
			}
			
		});
		//tableMenu.add(new JSeparator());
		tableMenu.add(hasAuthorTableMenu);
		//Has Subject
		JMenuItem hasSubjectTableMenu = new JMenuItem("Has Subject");
		hasSubjectTableMenu.addActionListener(new ActionListener(){
	
			@Override
			public void actionPerformed(ActionEvent e) {
				setTable(TableType.HASSUBJECT);
			}
			
		});
		//tableMenu.add(new JSeparator());
		tableMenu.add(hasSubjectTableMenu);
		//Book Copy
		JMenuItem bookCopyTableMenu = new JMenuItem("Book Copy");
		bookCopyTableMenu.addActionListener(new ActionListener(){
	
			@Override
			public void actionPerformed(ActionEvent e) {
				setTable(TableType.BOOKCOPY);
			}
			
		});
		//tableMenu.add(new JSeparator());
		tableMenu.add(bookCopyTableMenu);
		//Hold Request
		JMenuItem holdRequestTableMenu = new JMenuItem("Hold Request");
		holdRequestTableMenu.addActionListener(new ActionListener(){
	
			@Override
			public void actionPerformed(ActionEvent e) {
				setTable(TableType.HOLDREQUEST);
			}
			
		});
		//tableMenu.add(new JSeparator());
		tableMenu.add(holdRequestTableMenu);
		//Borrowing
		JMenuItem borrowingTableMenu = new JMenuItem("Borrowing");
		borrowingTableMenu.addActionListener(new ActionListener(){
	
			@Override
			public void actionPerformed(ActionEvent e) {
				setTable(TableType.BORROWING);
			}
			
		});
		//tableMenu.add(new JSeparator());
		tableMenu.add(borrowingTableMenu);
		//Fine
		JMenuItem fineTableMenu = new JMenuItem("Fine");
		fineTableMenu.addActionListener(new ActionListener(){
	
			@Override
			public void actionPerformed(ActionEvent e) {
				setTable(TableType.FINE);
			}
			
		});
		//tableMenu.add(new JSeparator());
		tableMenu.add(fineTableMenu);
		
		viewMenu.add(tableMenu);
		bar.add(viewMenu);
		
		//Command menu
		JMenu cmdMenu = new JMenu("Command");
		cmdMenu.setMnemonic(KeyEvent.VK_C);
		//Insert Menu Item
		JMenuItem insertMenu = new JMenuItem("Insert");
		insertMenu.setMnemonic(KeyEvent.VK_I);
		insertMenu.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String insertObj = (String)JOptionPane.showInputDialog(null, "Select:", "Insert Object", JOptionPane.QUESTION_MESSAGE, null, tableNames, tableNames[0]);
				if(insertObj != null){
					//Start DBObjectDialog
					System.out.println("Starting " + insertObj + " Dialog");
					resolveDBObjecViews(insertObj, DBOActionType.INSERT);
				}
			}
		});
		cmdMenu.add(insertMenu);
		//Update Menu Item
		JMenuItem updateMenu = new JMenuItem("Update");
		updateMenu.setMnemonic(KeyEvent.VK_U);
		updateMenu.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String updateObj = (String)JOptionPane.showInputDialog(null, "Select:", "Update Object", JOptionPane.QUESTION_MESSAGE, null, tableNames, tableNames[0]);
				if(updateObj != null){
					//Start DBObjectDialog
					System.out.println("Starting " + updateObj + " Dialog");
					resolveDBObjecViews(updateObj, DBOActionType.UPDATE);
				}
			}
		});
		//cmdMenu.add(new JSeparator());
		cmdMenu.add(updateMenu);
		JMenuItem deleteMenu = new JMenuItem("Delete");
		deleteMenu.setMnemonic(KeyEvent.VK_D);
		deleteMenu.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String deleteObj = (String)JOptionPane.showInputDialog(null, "Select:", "Delete Object", JOptionPane.QUESTION_MESSAGE, null, tableNames, tableNames[0]);
				if(deleteObj != null){
					//Start DBObjectDialog
					System.out.println("Starting " + deleteObj + " Dialog");
					resolveDBObjecViews(deleteObj, DBOActionType.DELETE);
				}
			}
		});
		//cmdMenu.add(new JSeparator());
		cmdMenu.add(deleteMenu);
		bar.add(cmdMenu);
		
		//Transaction Menu
		JMenu transMenu = new JMenu("Transaction");
		transMenu.setMnemonic(KeyEvent.VK_T);
		//Clerk Sub-Menu
		JMenuItem clerkMenuItem = new JMenuItem("Clerk");
		clerkMenuItem.setMnemonic(KeyEvent.VK_E);
		clerkMenuItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Starting Clerk Dialog");
				ViewResolver.gotoClerkView();
			}
			
		});
		transMenu.add(clerkMenuItem);
		//Borrower Sub-Menu
		JMenuItem borrowerMenuItem = new JMenuItem("Borrower");
		borrowerMenuItem.setMnemonic(KeyEvent.VK_W);
		borrowerMenuItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Starting Borrower Dialog");
				ViewResolver.gotoUserBorrowerView();
			}
			
		});
		transMenu.add(borrowerMenuItem);
		//Librarian Sub-Menu
		JMenuItem libMenuItem = new JMenuItem("Librarian");
		libMenuItem.setMnemonic(KeyEvent.VK_B);
		libMenuItem.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Starting Librarian Dialog");
				ViewResolver.gotoLibrarianView();
			}
			
		});
		transMenu.add(libMenuItem);
		bar.add(transMenu);

		setJMenuBar(bar);
	}
	
	private void resolveDBObjecViews(String objName, DBOActionType type){
		if(objName.equalsIgnoreCase("Borrower")) ViewResolver.gotoBorrowerView(type);
		else if(objName.equalsIgnoreCase("Borrower Type") || objName.equalsIgnoreCase("borrower_type")) ViewResolver.gotoBorrowerTypeView(type);
		else if(objName.equalsIgnoreCase("Book")) ViewResolver.gotoBookView(type);
		else if(objName.equalsIgnoreCase("Has Author") || objName.equalsIgnoreCase("hasauthor")) ViewResolver.gotoHasAuthorView(type);
		else if(objName.equalsIgnoreCase("Has Subject") || objName.equalsIgnoreCase("hassubject")) ViewResolver.gotoHasSubjectView(type);
		else if(objName.equalsIgnoreCase("Book Copy") || objName.equalsIgnoreCase("bookcopy")) ViewResolver.gotoBookCopyView(type);
		else if(objName.equalsIgnoreCase("Hold Request") || objName.equalsIgnoreCase("holdrequest")) ViewResolver.gotoHoldRequestView(type);
		else if(objName.equalsIgnoreCase("Borrowing")) ViewResolver.gotoBorrowingView(type);
		else if(objName.equalsIgnoreCase("Fine")) ViewResolver.gotoFineView(type);
		else appendText("Incorrect type. Type types to find out what types there are");
	}

	public void closeApplication(){
		LibraryDataAccessContext.closeConnection(IoC.getConnection());	//Close remaining connection
		appendText("Good Bye ... not that you can see this anyways .....\nIf you see this ... SOMETHING IS WRONG!!!\nRUN FOR YOUR LIVES!!!!");
		setVisible(false);
		dispose();
		System.exit(0);
	}
	
	public void updateDBTables() {
		//Create Tables
		borrowerTable = new DBTableView(new DBTableModel(TableType.BORROWER));
		borrower_typeTable = new DBTableView(new DBTableModel(TableType.BORROWERTYPE));
		bookTable = new DBTableView(new DBTableModel(TableType.BOOK));
		hasAuthorTable = new DBTableView(new DBTableModel(TableType.HASAUTHOR));
		hasSubjectTable = new DBTableView(new DBTableModel(TableType.HASSUBJECT));
		bookCopyTable = new DBTableView(new DBTableModel(TableType.BOOKCOPY));
		holdRequestTable = new DBTableView(new DBTableModel(TableType.HOLDREQUEST));
		borrowingTable = new DBTableView(new DBTableModel(TableType.BORROWING));
		fineTable = new DBTableView(new DBTableModel(TableType.FINE));
		
		DevDao devDao = IoC.resolve(DevDao.class);
		List<String[]> table_data = null;
		try {
			table_data = devDao.display(TableType.BORROWER);
			for(String[] rows : table_data) getTableModel(TableType.BORROWER).insertData(rows);
			table_data = devDao.display(TableType.BORROWERTYPE);
			for(String[] rows : table_data) getTableModel(TableType.BORROWERTYPE).insertData(rows);
			table_data = devDao.display(TableType.BOOK);
			for(String[] rows : table_data) getTableModel(TableType.BOOK).insertData(rows);
			table_data = devDao.display(TableType.HASSUBJECT);
			for(String[] rows : table_data) getTableModel(TableType.HASSUBJECT).insertData(rows);
			table_data = devDao.display(TableType.HASAUTHOR);
			for(String[] rows : table_data) getTableModel(TableType.HASAUTHOR).insertData(rows);
			table_data = devDao.display(TableType.BOOKCOPY);
			for(String[] rows : table_data) getTableModel(TableType.BOOKCOPY).insertData(rows);
			table_data = devDao.display(TableType.HOLDREQUEST);
			for(String[] rows : table_data) getTableModel(TableType.HOLDREQUEST).insertData(rows);
			table_data = devDao.display(TableType.BORROWING);
			for(String[] rows : table_data) getTableModel(TableType.BORROWING).insertData(rows);
			table_data = devDao.display(TableType.FINE);
			for(String[] rows : table_data) getTableModel(TableType.FINE).insertData(rows);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		if(table_data != null){
			for(String[] row : table_data){
				getTableModel(TableType.BOOK).insertData(row);
			}
		}
		if(currentTable == null) setTable(TableType.BORROWER);
		else setTable( ((DBTableModel)currentTable.getModel()).getType() );
	}

	public void appendText(String text){
		textArea.setText(textArea.getText()+"\n"+text);
	}
	
	public void clearText(){
		textArea.setText("");
	}
	
	public void setTable(TableType type){
		if(tableScrollPane != null) splitPane.remove(tableScrollPane);
		switch(type){
		case BORROWER:
			tableScrollPane = new JScrollPane(borrowerTable);
			currentTable = borrowerTable;
			break;
		case BORROWERTYPE:
			tableScrollPane = new JScrollPane(borrower_typeTable);
			currentTable = borrower_typeTable;
			break;
		case BOOK:
			tableScrollPane = new JScrollPane(bookTable);
			currentTable = bookTable;
			break;
		case HASAUTHOR:
			tableScrollPane = new JScrollPane(hasAuthorTable);
			currentTable = hasAuthorTable;
			break;
		case HASSUBJECT:
			tableScrollPane = new JScrollPane(hasSubjectTable);
			currentTable = hasSubjectTable;
			break;
		case BOOKCOPY:
			tableScrollPane = new JScrollPane(bookCopyTable);
			currentTable = bookCopyTable;
			break;
		case HOLDREQUEST:
			tableScrollPane = new JScrollPane(holdRequestTable);
			currentTable = holdRequestTable;
			break;
		case BORROWING:
			tableScrollPane = new JScrollPane(borrowingTable);
			currentTable = borrowingTable;
			break;
		case FINE:
			tableScrollPane = new JScrollPane(fineTable);
			currentTable = fineTable;
			break;
		}
		splitPane.add(tableScrollPane);
		validate();
		repaint();
	}
	
	public DBTableModel getTableModel(TableType type){
		switch(type){
		case BORROWER:
			return (DBTableModel) borrowerTable.getModel();
		case BORROWERTYPE:
			return (DBTableModel) borrower_typeTable.getModel();
		case BOOK:
			return (DBTableModel) bookTable.getModel();
		case HASAUTHOR:
			return (DBTableModel) hasAuthorTable.getModel();
		case HASSUBJECT:
			return (DBTableModel) hasSubjectTable.getModel();
		case BOOKCOPY:
			return (DBTableModel) bookCopyTable.getModel();
		case HOLDREQUEST:
			return (DBTableModel) holdRequestTable.getModel();
		case BORROWING:
			return (DBTableModel) borrowingTable.getModel();
		case FINE:
			return (DBTableModel) fineTable.getModel();
		}
		return null;
	}

}
